package com.tr.rentcar.model.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.tr.rentcar.model.GlobalParamHandler;
import com.tr.rentcar.model.bean.Cars;
import com.tr.rentcar.model.jdbc.MySqlDb;
import com.tr.rentcar.model.jdbc.ResultHandler;

/**
 * 车辆信息的数据处理
 * @author dell
 *
 */
public class CarInfoDao implements ResultHandler<Cars> {
	
	private MySqlDb db=MySqlDb.getInstance();

	@Override
	public Cars doHander(Map<String, Object> row) {
		Cars c=new Cars();
		c.setCid(Integer.parseInt(row.get("cid")+""));
		c.setCsid(Integer.parseInt(row.get("cs_id")+""));
		c.setCbid(Integer.parseInt(row.get("cb_id")+""));
		c.setCuid(Integer.parseInt(row.get("cu_id")+""));
		c.setCno(row.get("cno")+"");
		c.setCname(row.get("cname")+"");
		c.setCinfo(row.get("cinfo")+"");
		c.setCmoney(Double.parseDouble(row.get("cmoney")+""));
		c.setCstore(Integer.parseInt(row.get("cstore")+""));
		c.setCsale(Integer.parseInt(row.get("csale")+""));
		c.setCsName(row.get("cs_name")+"");
		c.setCbName(row.get("cb_name")+"");
		c.setCbLogo(row.get("cb_logo")+"");
		c.setCuName(row.get("cu_name")+"");
		c.setCphoto(row.get("cphoto")+"");
		c.setcJingpin(Integer.parseInt(row.get("c_jingpin")+""));
		c.setcNew(Integer.parseInt(row.get("c_new")+""));
		c.setcHot(Integer.parseInt(row.get("c_hot")+""));
		return c;
	}
	
	/**
	 * 修改车辆信息
	 */
	public boolean update1(String cname,String cno,Double cmoney,String cinfo,Integer Id)throws SQLException{
		String sql="update cars set cname=?,cno=?,cmoney=?,cinfo=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(cname,cno,cmoney,cinfo,Id))>0;
	}
	public boolean update2(String csid,Integer Id)throws SQLException{
		String sql="update cars set cs_id=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(csid,Id))>0;
	}
	public boolean update3(String cbid,Integer Id)throws SQLException{
		String sql="update cars set cb_id=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(cbid,Id))>0;
	}
	public boolean update4(String cuid,Integer Id)throws SQLException{
		String sql="update cars set cu_id=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(cuid,Id))>0;
	}
	/**
	 * 修改新品、热销、精品
	 */
	public boolean update5(Integer cid,String cjingpin,String cnew,String chot)throws SQLException{
		String sql="update cars set c_jingpin=?,c_new=?,c_hot=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(cjingpin,cnew,chot,cid))>0;
	}
	/**
	 * 修改新品
	 */
	public boolean update6(Integer cid,String lei)throws SQLException{
		String sql="update cars set c_new=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(lei,cid))>0;
	}
	/**
	 * 修改热销
	 */
	public boolean update7(Integer cid,String lei)throws SQLException{
		String sql="update cars set c_hot=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(lei,cid))>0;
	}
	/**
	 * 修改精品
	 */
	public boolean update8(Integer cid,String lei)throws SQLException{
		String sql="update cars set c_jingpin=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(lei,cid))>0;
	}
	/**
	 * 添加车辆信息
	 */
	public boolean save1(String cname,String cno,Double cmoney,String csid,String chot,String cnew,String cjingpin,String cinfo,String cuid,String cbid)throws SQLException{
		String sql="insert into cars(cname,cno,cmoney,cs_id,c_hot,c_new,c_jingpin,cinfo,cu_id,cb_id) values(?,?,?,?,?,?,?,?,?,?)";
		return db.executeUpdate(sql,new GlobalParamHandler(cname,cno,cmoney,csid,chot,cnew,cjingpin,cinfo,cuid,cbid))>0;
	}
	/**
	 * 修改图片
	 */
	public boolean updatephoto(String cphoto,Integer cId)throws SQLException{
		String sql="update cars set cphoto=? where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(cphoto,cId))>0;
	}
	/**
	 * 删除
	 */
	public boolean deletecar(Integer cId) throws SQLException{
		String sql="delete from cars where cid=?";
		return db.executeUpdate(sql,new GlobalParamHandler(cId))>0;
	}
	/**
	 * 获取车辆所有信息
	 * @return
	 * @throws SQLException
	 */
	public List<Cars> getAll() throws SQLException{
		String sql="select * from car_info_view";
		return db.executeQuery(sql, this);
	}
	/**
	 * 通过车辆编号或车辆名称查找
	 */
	public List<Cars> findbycno(String cno,String cno2) throws SQLException{
		String sql="select * from car_info_view where cno like ? or cname like ?";
		return db.executeQuery(sql,new GlobalParamHandler("%"+cno+"%","%"+cno2+"%"), this);
	}
	/**
	 * 获取车辆所有信息
	 * @return
	 * @throws SQLException
	 */
	public List<Cars> getAllLimit() throws SQLException{
		String sql="select * from car_info_view limit 0,5";
		return db.executeQuery(sql,this);
	}
	/**
	 * 获取精品车辆
	 * @return
	 * @throws SQLException
	 */
	public List<Cars> jingPin() throws SQLException{
		String sql="select * from cars WHERE c_jingpin=1 limit 0,6";
		return db.executeQuery(sql, this);
	}
	/**
	 * 获取新品车辆
	 * @return
	 * @throws SQLException
	 */
	public List<Cars> xingpin() throws SQLException{
		String sql="select * from cars WHERE c_new=1 limit 0,6";
		return db.executeQuery(sql, this);
	}
	/**
	 * 获取热销车辆
	 * @return
	 * @throws SQLException
	 */
	public List<Cars> rexiao() throws SQLException{
		String sql="select * from cars WHERE c_hot=1 limit 0,6";
		return db.executeQuery(sql, this);
	}
	/**
	 * 按ID查询车辆信息
	 * @param cid
	 * @return
	 * @throws SQLException
	 */
	public Cars getInfoById(String cid) throws SQLException{
		String sql="select * from car_info_view where cid=?";
		return db.queryUninResult(sql, new GlobalParamHandler(cid), this);
	}
	/**
	 * 获取车辆记录数
	 * @throws SQLException 
	 */
	public long getAllCount() throws SQLException{
		String sql="select count(*)as count from car_info_view";
		return (long) db.executeCountQuery(sql);
	}
	/**
	 * 筛选车辆信息
	 * @param csName
	 * @return
	 * @throws SQLException
	 */
	public List<Cars> getByCondition(String csName,String cbName,String beginMoney,String endMoney,String seq,int pageStart,int pageSize) throws SQLException{
		StringBuffer sql=new StringBuffer("select * from car_info_view where 1=1");
		//拼接sql语句
		sqlJoin(sql, csName, cbName, beginMoney, endMoney, seq);
		sql.append(" limit "+pageStart+","+pageSize);
		//System.out.println("sql语句："+sql.toString());
		return db.executeQuery(sql.toString(), this);
	}
	/**
	 * 按条件筛选记录数
	 * @param csName
	 * @param cbName
	 * @param beginMoney
	 * @param endMoney
	 * @param seq
	 * @return
	 * @throws SQLException
	 */
	public long getByConditionCount(String csName,String cbName,String beginMoney,String endMoney,String seq) throws SQLException{
		StringBuffer sql=new StringBuffer("select count(*)as count from car_info_view where 1=1");
		//拼接sql语句
		sqlJoin(sql, csName, cbName, beginMoney, endMoney, seq);
		return (long) db.executeCountQuery(sql.toString());
	}
	/**
	 * 拼接sql语句
	 * @param sql
	 * @param csName
	 * @param cbName
	 * @param beginMoney
	 * @param endMoney
	 * @param seq
	 */
	private void sqlJoin(StringBuffer sql,String csName,String cbName,String beginMoney,String endMoney,String seq){
		if(!"全部".equals(csName)){
			sql.append(" and cs_name='").append(csName).append("'");
		}
		if(!"'全部'".equals(cbName)){
			sql.append(" and cb_name in (").append(cbName).append(")");
		}
		if(!"全部".equals(beginMoney) && !"全部".equals(endMoney)){
			sql.append(" and cmoney between ").append(beginMoney).append(" and ").append(endMoney);
		}
		if("按租金".equals(seq)){
			sql.append(" order by cmoney,c_hot desc");
		}else if("按销量".equals(seq)){
			sql.append(" order by csale,c_hot desc");
		}else{
			sql.append(" order by c_hot desc");
		}
		
	}
}
